package com.arron.juc;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Classname AtomicReferenceDemo
 * @Description TODO
 * @Date 2020/8/20 22:39
 * @Created by 刘
 */

class User{
    private String name;
    private Integer age;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class AtomicReferenceDemo {

    public static void main(String[] args) {

        User z3 = new User("z3", 21);
        User li4 = new User("li4", 27);
        AtomicReference<User> userAtomicReference = new AtomicReference<>(z3);
        System.out.println(userAtomicReference.compareAndSet(z3, li4)+"\t"+userAtomicReference.get());
        System.out.println(userAtomicReference.compareAndSet(li4, z3)+"\t"+userAtomicReference.get());

    }
}
